﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Settings;

namespace djs_mp3_manager.Nastroje
{
    public partial class Moznosti : Form
    {
        Nastroje.MoznostiTabs.Enviroment enviroment = null;
        Nastroje.MoznostiTabs.Genres genres = null;
        Nastroje.MoznostiTabs.Folders folders = null;

        public delegate void OnSaveDelegate();
        public event OnSaveDelegate OnSaveEvent;

        public Moznosti()
        {
            InitializeComponent();
        }

        private void Moznosti_Load(object sender, EventArgs e)
        {
            LanguageManager.SetLocalization(this);
            enviroment = new Nastroje.MoznostiTabs.Enviroment();
            enviroment.OnChangeEvent += new Nastroje.MoznostiTabs.OnChangeDelegate(OnChanged_event);
            genres = new Nastroje.MoznostiTabs.Genres();
            genres.OnChangeEvent += new Nastroje.MoznostiTabs.OnChangeDelegate(OnChanged_event);
            folders = new Nastroje.MoznostiTabs.Folders();
            folders.OnChangeEvent += new Nastroje.MoznostiTabs.OnChangeDelegate(OnChanged_event);
            optionsTree.Nodes[0].Expand();
            
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            UpdateSettingsValues();
            Setting.SaveSetting();
            this.Close();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Name == "Enviroment")
            {
                this.splitContainer2.Panel1.Controls.Clear();
                this.splitContainer2.Panel1.Controls.Add(enviroment);
            } else if (e.Node.Name == "Genres") {
                this.splitContainer2.Panel1.Controls.Clear();
                this.splitContainer2.Panel1.Controls.Add(genres);
            } else if (e.Node.Name == "Folders") {
                this.splitContainer2.Panel1.Controls.Clear();
                this.splitContainer2.Panel1.Controls.Add(folders);
            }
        }

        private void treeView1_BeforeCollapse(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Index == 0)
            {
                e.Cancel = true;
            }
        }

        private void applyButton_Click(object sender, EventArgs e)
        {
            UpdateSettingsValues();
            Setting.SaveSetting();
            applyButton.Enabled = false;
        }

        private void Moznosti_FormClosing(object sender, FormClosingEventArgs e)
        {
            enviroment.Dispose();
            folders.Dispose();
            genres.Dispose();
        }

        private void UpdateSettingsValues()
        {
            #region Enviroment Tab
            if (enviroment.ListOfChanged.Count > 0)
            {
                List<MoznostiTabs.Enviroment.TypesForChange> changed = enviroment.ListOfChanged;

                foreach (MoznostiTabs.Enviroment.TypesForChange item in changed)
                {
                    switch(item)
                    {
                        case MoznostiTabs.Enviroment.TypesForChange.LanguageFile:
                            {
                                Setting.Enviroment.Lang = enviroment.LangFile;
                                break;
                            }
                    }
                }
            }
            #endregion
        
            #region Folders
            if (folders.ListOfChanged.Count > 0)
            {
                List<MoznostiTabs.Folders.TypesForChange> changed = folders.ListOfChanged;

                foreach (MoznostiTabs.Folders.TypesForChange item in changed)
                {
                    switch (item)
                    {
                        case MoznostiTabs.Folders.TypesForChange.SelectedFolders:
                            {
                                Setting.Folders.SelectedFolders = folders.SelectedFolders;
                                break;
                            }
                    }
                }
            }
            #endregion

            #region Genres
            if (genres.ListOfChanged.Count > 0)
            {
                List<MoznostiTabs.Genres.TypesForChange> changed = genres.ListOfChanged;

                foreach (MoznostiTabs.Genres.TypesForChange item in changed)
                {
                    switch (item)
                    {
                        case MoznostiTabs.Genres.TypesForChange.GenreTree:
                            {
                                Setting.Genres.GenreList = genres.GenreTree;
                                break;
                            }
                    }
                }
            }
            
            #endregion

            #region ClearingChanged
            enviroment.RemoveChanged();
            folders.RemoveChanged();
            genres.RemoveChanged();
            #endregion

            if (OnSaveEvent != null)
            {
                OnSaveEvent();
            }
        }

        protected void OnChanged_event()
        {
            applyButton.Enabled = true;
        }

    }
}
